package com.yanzhang.service.user;

import com.yanzhang.dao.user.UserDao;
import com.yanzhang.dao.user.UserDaoImpl;
import com.yanzhang.pojo.User;
import com.yanzhang.util.DBUtil;
import com.yanzhang.util.DateUtil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yanzhang
 */
public class UserServiceImpl implements UserService{
    private UserDao userDao;

    public UserServiceImpl () {
        userDao = new UserDaoImpl();
    }

    /**
     * 新增一个用户
     * @param user 用户对象
     * @return 是否成功
     */
    @Override
    public boolean add(User user) {
        boolean flag = false;
        Connection connection = DBUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int rs = userDao.add(connection, user);
            connection.commit();
            if (rs > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("新增用户失败：事务回滚");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
        return flag;
    }

    /**
     * 获取登录用户
     * @param userCode 用户编码
     * @param password 用户密码
     * @return 用户对象
     */
    @Override
    public User login(String userCode, String password) {
        User user = null;
        Connection connection = DBUtil.getConnection();
        try {
            user = userDao.getUserByCode(connection, userCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(connection);
        }
        //校验密码
        if (user != null) {
            if (!user.getUserPassword().equals(password)) {
                System.out.println("user置为空");
                user = null;
            }
        }
        return user;
    }

    /**
     * 分页查询用户集合
     * @param userName      用户名
     * @param userRole      角色id
     * @param currentPageNo 当前页码
     * @param pageSize      每页数量
     * @return 用户对象集合
     */
    @Override
    public List<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) {
        List<User> userList = new ArrayList<>();
        Connection connection = DBUtil.getConnection();
        try {
            userList = userDao.getUserList(connection, userName, userRole, currentPageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(connection);
        }
        return userList;
    }

    /**
     * 查询用户总数量
     * @param userName 用户名
     * @param userRole 角色id
     * @return 结果数量
     */
    @Override
    public int getUserCount(String userName, int userRole) {
        int count = 0;
        Connection connection = DBUtil.getConnection();
        try {
            count = userDao.getUserCount(connection, userName, userRole);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(connection);
        }
        return count;
    }

    /**
     * 通过id获取用户
     * @param id 主键
     * @return 用户对象
     */
    @Override
    public User getUserById(int id) {
        User user = null;
        Connection connection = DBUtil.getConnection();
        try {
            user = userDao.getUserById(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(connection);
        }
        return user;
    }

    /**
     * 通过用户编码获取用户
     * @param userCode 用户编码
     * @return 用户对象
     */
    @Override
    public User getUserByCode(String userCode) {
        User user = null;
        Connection connection = DBUtil.getConnection();
        try {
            user = userDao.getUserByCode(connection, userCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(connection);
        }
        return user;
    }

    /**
     * 修改用户信息
     * @param user 用户对象
     * @return 是否成功
     */
    @Override
    public boolean modifyUser(User user) {
        boolean flag = false;
        Connection connection = DBUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int rs = userDao.modifyUser(connection, user);
            connection.commit();
            if (rs > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("修改用户失败：事务回滚");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
        return flag;
    }

    /**
     * 修改用户密码
     * @param id 主键
     * @param password 新密码
     * @return 是否成功
     */
    @Override
    public boolean updatePassword(int id, String password) {
        boolean flag = false;
        Connection mysqlConnection = DBUtil.getConnection();
        try {
            if (userDao.updatePassword(mysqlConnection, id, password) > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(mysqlConnection);
        }
        return flag;
    }

    /**
     * 通过id删除用户
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteUserById(int id) {
        boolean flag = false;
        Connection connection = DBUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int rs = userDao.deleteUserById(connection, id);
            connection.commit();
            if (rs > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("删除用户失败：事务回滚");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
        return flag;
    }


    @Test
    public void testLogin() {
        User user = login("admin", "1234567");
        System.out.println(user.getUserPassword());
    }

    @Test
    public void testUpdatePassword() {
        boolean flag = updatePassword(1, "12345678");
        System.out.println(flag);
    }

    @Test
    public void testAddUser() {
        User user = new User();
        user.setUserCode("huge");
        user.setUserName("胡歌");
        user.setUserPassword("12345678");
        user.setGender(1);
        try {
            user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1986-05-23"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setPhone("18866227853");
        user.setAddress("广东省广州市白云区");
        user.setUserRole(2);
        user.setCreationDate(new Date());
        user.setCreatedBy(1);
        boolean flag = add(user);
        System.out.println(flag);
    }

    @Test
    public void testModifyUser() {
        User user = getUserById(17);
        System.out.println(user.getUserName());
        user.setBirthday(DateUtil.getDate("1986-08-25", DateUtil.Time_Zone.SYSTEM));
        user.setPhone("18866227859");
        user.setAddress("广东省广州市白云区京溪路");
        user.setModifyDate(new Date());
        user.setModifyBy(1);
        boolean flag = modifyUser(user);
        System.out.println(flag);
    }

    @Test
    public void testDeleteUser() {
        boolean flag = deleteUserById(16);
        System.out.println(flag);
    }
}
